﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using YArchitech.Controls;
using YArchitech.Public;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	public class YShapeTeeAlignment : TeeConnAlignment
	{
		public YShapeTeeAlignment(Document doc) : base(doc)
		{
		}

		public override bool connectAlignment(List<Duct> ducts, List<XYZ> points)
		{
			bool result = false;
			SubTransaction subTransaction = null;
			try
			{
				if (!this.MeetFittingCondition(ducts, points))
				{
					return false;
				}
				List<Duct> list = ducts;
				List<Alignments> list2;
				if (ducts[1].UniqueId == ducts[2].UniqueId)
				{
					list2 = this.GetAlignmentBeforeSperate(ducts);
					list = this.GetNewdDucts(ducts, points);
					if (list == null || list2 == null)
					{
						return false;
					}
				}
				else
				{
					list2 = this.GetAlignment(list);
				}
				if (list2 == null || list2.Count != 2)
				{
					base.ErrorString = "风管没有正确的对齐方式";
					return false;
				}
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(list[0], points[0]);
				YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(list[1]);
				YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(list[2]);
				Connector closeConnector = hypiping.GetCloseConnector(hypiping3.StartConnector.Origin, false);
				Connector closeConnector2 = hypiping3.GetCloseConnector(hypiping.StartConnector.Origin, false);
				Connector closeConnector3 = hypiping2.GetCloseConnector(hypiping.StartConnector.Origin, false);
				if (closeConnector.IsConnected || closeConnector3.IsConnected || closeConnector2.IsConnected)
				{
					YJKMessageBox.Show("选择的风管已经被连接，请重新选择风管");
					return false;
				}
				NineAlignments oneAlignInNineAlignments = base.GetOneAlignInNineAlignments(list2);
				string familyName = "";
				bool familyName2 = this.GetFamilyName(list, oneAlignInNineAlignments, ref familyName);
				FamilyInstance familyInstance = base.GetFamilyInstance(base.Doc, list, familyName, familyName2);
				if (familyInstance == null)
				{
					base.ErrorString = "没有找到相应的族，连接失败";
					return false;
				}
				base.RotateElementToAlignAllDucts(familyInstance, closeConnector, closeConnector3, closeConnector2, base.Doc);
				subTransaction = new SubTransaction(base.Doc);
				subTransaction.Start();
				List<Connector> listConn = new List<Connector>
				{
					closeConnector,
					closeConnector3,
					closeConnector2
				};
				base.ModifySizeAndConnect(listConn, familyInstance);
				subTransaction.Commit();
				result = true;
			}
			catch (Exception ex)
			{
				base.ErrorString = ex.ToString();
				return false;
			}
			finally
			{
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
                    if (3 != (int)status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return result;
		}

		public override bool MeetFittingCondition(List<Duct> ducts, List<XYZ> points)
		{
			if (ducts.Count != 3 || points.Count != 3)
			{
				base.ErrorString = "三通连接必须是三条风管！";
				return false;
			}
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0], points[0]);
			YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1], points[1]);
			YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(ducts[2], points[2]);
			if (hypiping.Piping.Id.Equals(hypiping2.Piping.Id) && hypiping.Piping.Id.Equals(hypiping3.Piping.Id))
			{
				base.ErrorString = "同一根风管不能构成三通连接！";
				return false;
			}
			if (hypiping.Piping.Id.Equals(hypiping3.Piping.Id))
			{
				base.ErrorString = "第一根风管与第三根风管不能为同一风管！";
				return false;
			}
			if (hypiping.Piping.Id.Equals(hypiping2.Piping.Id))
			{
				base.ErrorString = "第一根风管与第二根风管不能为同一风管！";
				return false;
			}
			if (!hypiping.IsVertical(ducts[2]))
			{
				base.ErrorString = "第一根风管与第三根风管必须垂直！";
				return false;
			}
			if (!hypiping.IsVertical(ducts[1]))
			{
				base.ErrorString = "第一根风管与第二根风管必须垂直！";
				return false;
			}
			return true;
		}

		public override List<Alignments> GetAlignment(List<Duct> ducts)
		{
			return base.GetAlignmentByYShapeTee(ducts);
		}

		public override List<Duct> GetNewdDucts(List<Duct> ducts, List<XYZ> points)
		{
			List<Duct> ductsBySperateDuct = base.GetDuctsBySperateDuct(ducts[2], ducts[0], points[1]);
			if (ductsBySperateDuct == null)
			{
				return null;
			}
			return new List<Duct>
			{
				ducts[0],
				ductsBySperateDuct[0],
				ductsBySperateDuct[1]
			};
		}

		public override bool GetFamilyName(List<Duct> ducts, NineAlignments na, ref string familyName)
		{
			List<string> nineFamilyNames = this.GetNineFamilyNames();
			switch (na)
			{
			case NineAlignments.TopLeft:
				familyName = nineFamilyNames[0];
				break;
			case NineAlignments.TopRight:
				familyName = nineFamilyNames[1];
				break;
			case NineAlignments.TopMiddle:
				familyName = nineFamilyNames[2];
				break;
			case NineAlignments.BottomLeft:
				familyName = nineFamilyNames[3];
				break;
			case NineAlignments.BottomRight:
				familyName = nineFamilyNames[4];
				break;
			case NineAlignments.BottomMiddle:
				familyName = nineFamilyNames[5];
				break;
			case NineAlignments.MiddleLeft:
				familyName = nineFamilyNames[6];
				break;
			case NineAlignments.MiddleRight:
				familyName = nineFamilyNames[7];
				break;
			case NineAlignments.MiddleMiddle:
				familyName = nineFamilyNames[8];
				break;
			}
			return false;
		}

		public override List<string> GetNineFamilyNames()
		{
			return new List<string>
			{
				"矩形Y型三通_水平左_竖直顶对齐_法兰",
				"矩形Y型三通_水平右_竖直顶对齐_法兰",
				"矩形Y型三通_水平中_竖直顶对齐_法兰",
				"矩形Y型三通_水平左_竖直底对齐_法兰",
				"矩形Y型三通_水平右_竖直底对齐_法兰",
				"矩形Y型三通_水平中_竖直底对齐_法兰",
				"矩形Y型三通_水平左_竖直中对齐_法兰",
				"矩形Y型三通_水平右_竖直中对齐_法兰",
				"矩形Y型三通_水平中_竖直中对齐_法兰"
			};
		}
	}
}
